package color.koitq.picker.adapter

import android.graphics.Color
import android.view.View
import color.koitq.picker.R
import color.koitq.picker.adapter.HomeAdapter.HomeItemClickListener
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class HomeColorAdapter : BaseQuickAdapter<String?, BaseViewHolder>(R.layout.item_home_color) {
    private var listener: HomeItemColorClickListener? = null

    fun listener(homeItemClickListener: HomeItemColorClickListener?): HomeColorAdapter? {
        listener = homeItemClickListener
        return this
    }

    override fun convert(baseViewHolder: BaseViewHolder, s: String?) {
        baseViewHolder.setText(R.id.tv_hex, s)
        val color = Color.parseColor(s)
        val red: Int = color and 0xff0000 shr 16
        val green: Int = color and 0x00ff00 shr 8
        val blue: Int = color and 0x0000ff

        baseViewHolder.setText(
            R.id.tv_rgb,
            "$red,$green,$blue"
        )
        baseViewHolder.setBackgroundColor(R.id.view, Color.parseColor(s))
        baseViewHolder.getView<View>(R.id.view1).setOnClickListener { v: View? ->
            listener?.click(s)
        }
         baseViewHolder.getView<View>(R.id.view2).setOnClickListener { v: View? ->
            listener?.click("$red,$green,$blue")
        }

    }
    interface HomeItemColorClickListener {
        fun click(s: String?)
    }
}